﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ASMS.Excel2DbTool.Utils
{
    /// <summary>
    /// 仓库数据各省仓库数排行
    /// </summary>
    public class 仓库数据各省仓库数排行
    {
        public static void Run(string filePath, string destFileName)
        {
            DataTable dataTable = Sunboxsoft.Excel.ReadTool.ReadExcel(filePath, true);

            StringBuilder xValuesBuilder = new StringBuilder("[");
            StringBuilder yValuesBuilder = new StringBuilder("[");

            List<string> xValues = new List<string>();
            List<decimal> yValues = new List<decimal>();

            for (int index = 0; index < dataTable.Rows.Count; index++)
            {
                string nameString = Convert.ToString(dataTable.Rows[index][0]).Trim();
                string valueString = Convert.ToString(dataTable.Rows[index][1]).Trim();

                if (string.IsNullOrEmpty(nameString)
                    || string.IsNullOrEmpty(valueString))
                    continue;

                decimal value = 0;
                if (decimal.TryParse(valueString, out value))
                {
                    bool isInsert = false;
                    for (int yIndex = 0; yIndex < yValues.Count; yIndex++)
                    {
                        if (yValues[yIndex] < value)
                        {
                            yValues.Insert(yIndex, value);
                            xValues.Insert(yIndex, nameString);
                            isInsert = true;
                            break;
                        }
                    }
                    if (isInsert == false)
                    {
                        yValues.Add(value);
                        xValues.Add(nameString);
                    }
                }
            }

            for (int index = 0; index < 10; index++)
            {
                if (index == 0)
                {
                    xValuesBuilder.AppendFormat("\"{0}\"", xValues[index]);
                    yValuesBuilder.AppendFormat("{0}", yValues[index]);
                }
                else
                {
                    xValuesBuilder.AppendFormat(",\"{0}\"", xValues[index]);
                    yValuesBuilder.AppendFormat(",{0}", yValues[index]);
                }
            }
            xValuesBuilder.Append("]");
            yValuesBuilder.Append("]");
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat("{{\"xValues\":{0},\"yValues\":{1}}}", xValuesBuilder, yValuesBuilder);

            string destFilePath = string.Format("{0}\\{1}", AppDomain.CurrentDomain.BaseDirectory, destFileName);
            System.IO.File.WriteAllText(destFilePath, builder.ToString());
            Console.WriteLine(string.Format("转换完毕{0},count:{1}", destFilePath, dataTable.Rows.Count));
        }
    }
}